<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Frag;
use App\Models\MClassify;
use App\Models\MFeedback;
use App\Models\MNews;
use App\Models\MNotices;
use Illuminate\Http\Request;
use Storage;

/**
 * 信息管理控制器
 */
class ContentController extends Controller
{

    /**
     * 会员咨询列表
     * $flag = 0(未回复咨询)
     * $flag = 1(已回复咨询)
     */
    public function feedbackPage(Request $request, $flag = 0, $index = 1)
    {
        //开启分页搜索支持
        $this->fyInit(route('admin.feedback', ['flag' => $flag]), 20, $request->getQueryString());
        $query = (new MFeedback())->query()->where("status", $flag);

        //按时间过滤
        if ($request->has(['start', 'end'])) {
            $query = $query->where('time', '>=', strtotime($request->start))
                ->where('time', '<=', strtotime($request->end));
        }

        //按会员帐号搜索
        if ($request->has('search_type') && $request->search_type == 1) {
            $query = $query->where('uname', 'like', "%" . $request->search_str . "%");
        }

        //按用户ID搜索
        if ($request->has('search_type') && $request->search_type == 2) {
            $query = $query->where('uid', $request->search_str);
        }

        //按咨询内容搜索
        if ($request->has('search_type') && $request->search_type == 3) {
            $query = $query->where('content', 'like', "%" . $request->search_str . "%");
        }

        //按咨询ID搜索
        if ($request->has('search_type') && $request->search_type == 4) {
            $query = $query->where('id', $request->search_str);
        }

        //排序
        if ($request->has('sort_type')) {
            if ($request->sort_type == 1) {         //按修改时间降序
                $query = $query->orderBy("time", "desc");
            } elseif ($request->sort_type == 2) {   //按修改时间升序
                $query = $query->orderBy("time", "asc");
            }
        }

        $this->fyCalc($query->count(), $index);
        $this->data['feedbacks'] = $query->skip($this->fy_pgOffset)
            ->take($this->fy_pgSize)
            ->where('status', $flag)
            ->orderBy('time', 'desc')
            ->get();

        $this->fyViewInit($this->data);

        $this->data['flag'] = $flag;
        return view('admin.feedbacks')->with($this->data);
    }

    /**
     * 会员咨询回复界面
     */
    public function feedbackEditPage(Request $request, $id, $index = 1)
    {
        //开启分页
        $this->fyInit(route('admin.feedback.edit', ['id' => $id]), 5, $request->getQueryString());
        $query = (new MFeedback())->query();

        $this->fyCalc($query->count(), $index);
        $this->data['feedback_reply'] = $query->skip($this->fy_pgOffset)->take($this->fy_pgSize)->orderBy('time', 'desc')->get();
        $this->fyViewInit($this->data);

        $this->data['id'] = $id;
        return view('admin.feedback_edit')->with($this->data);
    }

    /**
     * 回复会员咨询信息保存
     */
    public function feedbackReply(Request $request, $id)
    {
        if (!$this->validateReq($request, MFeedback::editRulesWithMsg(0), MFeedback::editRulesWithMsg(1))) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return response()->json($this->data);
        }

        $reply = MFeedback::find($id);
        if (empty($reply)) {
            $reply = new MFeedback();
        }

        if ($reply->saveData($request)) {
            $this->data = $this->makeJsCode(0, "回复成功！");
        } else {
            $this->data = $this->makeJsCode(1, "回复失败！");
        }

        return response()->json($this->data);
    }

    /**
     * 删除会员咨询
     */
    public function feedbackDel($id)
    {
        MFeedback::destroy($id);
        $this->data = $this->makeJsCode(0, "删除成功！");
        return back()->with($this->data);
    }

    /**
     * 碎片显示界面
     */
    public function fragPage(Request $request, $index=1)
    {
        //开启分页搜索支持
        $this->fyInit(route("admin.frag"), 10, $request->getQueryString());
        $query = (new Frag())->query();

        //按碎片位置筛选
        if ($request->has('search_type') && $request->search_type == 0) {
            $query = $query->where('position', 'like', "%" . $request->search_str . "%");
        }

        //按碎片标示筛选
        if ($request->has('search_type') && $request->search_type == 1) {
            $query = $query->where('slug', 'like', "%" . $request->search_str . "%");
        }

        //按时间过滤
        if ($request->has(['start', 'end'])) {
            $query = $query->where('create_time', '>=', strtotime($request->start))
                ->where('create_time', '<=', strtotime($request->end));
        }

        //排序
        if ($request->has('sort_type')) {
            if ($request->sort_type == 1) {//按id降序
                $query = $query->orderBy("id", "desc");
            } elseif ($request->sort_type == 2) {//按id升序
                $query = $query->orderBy("id", "asc");
            }
        }

        $this->fyCalc($query->count(), $index);
        $this->data['frags'] = $query->skip($this->fy_pgOffset)
            ->take($this->fy_pgSize)
            ->get();

        $this->fyViewInit($this->data);

          return view('admin.frag')->with($this->data);
    }

    /**
     * 碎片编辑界面
     */
    public function fragEditPage ($id=0)
    {
        $this->data['frag'] = Frag::find($id);
        return view ('admin.frag_edit')->with($this->data);
    }

    /**
     * 碎片保存
     */
    public function fragSave (Request $request,$id=0)
    {
        if (!$this->validateReq($request, Frag::fragRulesWithMsg(0), Frag::fragRulesWithMsg(1))) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return response()->json($this->data);
        }

        if (!empty($request->picture)) { //存储图片
            Storage::put(
                $request->slug.'.jpg',
                file_get_contents($request->file('picture')->getRealPath())
            );
        }

        $frag = Frag::find($id);
        if (empty($frag)) { //判断添加还是修改
            $frag = new Frag;
        }
//        $picture = $frag->picture;
//        var_dump($picture);
//
////        var_dump($frag);
//        die;
        if (Frag::saveFrag($request,$frag)) {
            $this->data = $this->makeJsCode(0,'碎片保存成功');
        } else {
            $this->data = $this->makeJsCode(1,'碎片保存失败');
        }
        return response()->json($this->data);
    }

    /**
     * 碎片删除
     */
    public function fragDel ($id)
    {
        $frag = Frag::find($id);
        if ($frag->delete()) {
            $this->data = $this->makeJsCode(0,'碎片删除成功');
        } else {
            $this->data = $this->makeJsCode(1,'碎片删除失败');
        }
        return back()->with($this->data);
    }
}